package org.fpp.proxy;

import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author bigbird-0101
 * @date 2024-06-25 21:58
 */
public class JdkDynamicProxy {
    public static void main(String[] args) {
//        new UndeclaredThrowableException(new RuntimeException());
        ClassLoader classLoader = JdkDynamicProxy.class.getClassLoader();
        MaiPiao proxyMaiPiao = (MaiPiao) Proxy.newProxyInstance(classLoader, new Class[]{MaiPiao.class}, (proxy, method, args1) -> {
            if(method.getName().equals("maiPiao")){
                System.out.println("卖票");
                return null;
            }else if(method.getName().equals("getName")){
                return "name1";
            }
            return method.invoke(proxy);
        });
        proxyMaiPiao.maiPiao();
        String name = proxyMaiPiao.getName();
        System.out.println(name);
        proxyMaiPiao.doSomething();
    }

    public interface MaiPiao{
        void maiPiao();

        String getName();

        void doSomething();
    }
}
